package com.wh.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.common.dao.NoticeMapper;
import com.wh.common.dto.NoticeDto;
import com.wh.common.pojo.Notice;
import com.wh.common.pojo.Task;
import com.wh.common.service.NoticeService;
import com.wh.org.dao.IEmployeeDao;
import com.wh.sse.controller.SseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 1
 * @since 2024-06-21 星期五 18:34:37
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private IEmployeeDao iEmployeeDao;

    @Autowired
    private SseController sseController;

    /**
     * 获取公告分页列表
     *
     * @param noticeDto
     * @return
     */
    @Override
    public IPage<Notice> findByPage(NoticeDto noticeDto) {
        //1.创建查询条件
        QueryWrapper<Notice> qw = new QueryWrapper<>();
        //2
        String title = noticeDto.getRuleSelectForm().getTitle();
        String state = noticeDto.getRuleSelectForm().getState();
        //TODO 这个获取的是名字吧，数据库设计是员工id
//        Integer creator = noticeDto.getRuleSelectForm().getCreator();
        //3
        if (!title.isEmpty()) {
            qw.like("title", title);
        }
        if (!state.isEmpty()) {
            qw.eq("state", state);
        }

        //4.组件分页数据
        IPage<Notice> page = new Page<>(noticeDto.getCurrentPage(), noticeDto.getPageSize());
        page.setRecords(noticeMapper.findByPage(page, qw));
        page.setTotal(noticeMapper.selectCount(qw));
        //3.返回
        return page;
    }

    @Override
    public void add(Notice notice) {
        Long creatorLogininfoId = notice.getCreator();//前端传递过来的登陆logininfoId;
        Long creatorId = iEmployeeDao.findEmployeeIdByloginId(creatorLogininfoId);//获取真实的登陆人id;
        notice.setCreator(creatorId);
        //2 添加创建时间
        notice.setCreateTime(LocalDateTime.now());
        notice.setTop(0);
        //3落库
        noticeMapper.insert(notice);
    }

    @Transactional
    @Override
    public void updNotice(Notice notice) {
        //1获取此条公告的id
        Integer id = notice.getId();
        //2根据id搜索数据库对象
        Notice notice1 = noticeMapper.selectById(id);
        //3设置数据
        notice1.setNoticeText(notice.getNoticeText());
        notice1.setTitle(notice.getTitle());
        notice1.setState(notice.getState());
        //4落库
        noticeMapper.updateById(notice1);

    }

    /**
     * 是否置顶 修改置顶状态
     */
    @Override
    @Transactional
    public void createTop(Integer noticeId)  {
        //新的要置顶公告
        Notice newNotice = noticeMapper.selectById(noticeId);
        //查询之前的是否有置顶的公告,如果有则置为0
        QueryWrapper<Notice> qw=new QueryWrapper<>();
        qw.eq("top",1);
        Notice oldnotice = noticeMapper.selectOne(qw);//查询之前置顶的
        if(oldnotice !=null) {
            //存在已经置顶公告,则取消置顶
            oldnotice.setTop(0);
            noticeMapper.updateById(oldnotice);
        }
        //如果不存在则直接进行置顶
        newNotice.setTop(1);
        noticeMapper.updateById(newNotice);
        //发送sse消息推广
        //获取消息信息
        String newNoticeNoticeText = newNotice.getNoticeText();
        sseController.sendAnnouncement(newNoticeNoticeText);
    }

    /**
     * 查询置顶数据
     *
     * @return
     */
    //初始化查询置顶
    public String getTopData()  {
        // 1. 查询是否有置顶的数据
        QueryWrapper<Notice> qw = new QueryWrapper<>();
        qw.eq("top", "1");
        List<Notice> noticeList = noticeMapper.selectList(qw);

        // 2. 判断是否有数据
        Notice notice;
        if (noticeList.isEmpty()) { // 使用 isEmpty() 方法检查列表是否为空
            // 说明没有置顶，查询最近一个时间的公告内容
            notice = noticeMapper.selectLastTime();
        } else {
            // 说明有置顶，获取第一个置顶的公告内容
            notice = noticeList.get(0);
        }
        return notice.getNoticeText();
    }

    /**
     * 取消置顶公告
     * @param id
     */
    @Override
    public void downTop(Integer id) {
        //将置顶改为0
        Integer top=0;
        noticeMapper.updateTop(id,top);
    }


    @Override
    public void deleByid(Integer noticeId) {
        noticeMapper.deleteById(noticeId);
    }

    @Override
    public void insert(Task task) {

    }


}
